﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SettlementDetailShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (Request.QueryString["DetailID"] != null)
                {
                    if (mk.IsNumber(Request.QueryString["DetailID"].ToString()))
                    {
                        //用一个隐藏Label保存传递过来的ID
                        this.lblDetailID.Text = Request.QueryString["DetailID"].ToString();
                    }
                    else
                    {
                        mk.Alert("参数错误！");
                    }
                }
                else
                {
                    mk.Alert("参数错误！");
                }
                //设置经办人
              Accounts_Users currentUser =mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j => j.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                //绑定页面基本数据
                this.BindData();

                ////设置经办人
                //UART.Accounts.Bus.User currentUser = (UART.Accounts.Bus.User)(Session["UserInfo"]);
                //this.lblCurrentUser.Text = currentUser.UserName;
                //this.lblManager.Text = currentUser.TrueName;
            }
        }
          private void BindData()
        {
         var bll = mk.l.Sales_SettlementDetail.ToList();
           Sales_SettlementDetail m = bll.Where(h=>h.DetailID==int.Parse(this.lblDetailID.Text)).FirstOrDefault();
            if (m != null)
            {
                if (int.Parse(this.lblRootDepartmentID.Text) == m.RootDepartmentID)
                {
                    switch (m.Flag)
                    {
                        case "1":
                            this.lblFlag.Text = "支出";
                            break;
                        case "2":
                            this.lblFlag.Text = "收入";
                            break;
                        default:
                            lblFlag.Text = "未知";
                            break;
                    }
                   var bllType = mk.l.Sales_SettlementType.ToList();
              Sales_SettlementType mType = bllType.Where(h=>h.TypeID==m.TypeID).FirstOrDefault();
                    this.lblType.Text = mType.Name;
                    this.lblDateReckoning.Text = m.DateReckoning.ToString();
                    this.lblInfoFrom.Text = m.InfoFrom;
                    this.lblInfoTo.Text = m.InfoTo;
                    //显示订单编号
                    if (m.SalesOrderID != 0)
                    {
                        var bllSalesOrderHeader =mk.l.Sales_SalesOrderHeader.ToList();
                               Sales_SalesOrderHeader mSalesOrderHeader = bllSalesOrderHeader.Where(h=>h.SalesOrderID==m.SalesOrderID).FirstOrDefault();
                        this.lblSalesOrderNumber.Text = mSalesOrderHeader.SalesOrderNumber;
                    }
                    else
                    {
                        this.lblSalesOrderNumber.Text = "无";
                    }
                    //显示订货单编号
                    if (m.PurchaseOrderID != 0)
                    {
                       var bllPurchaseOrderHeader = mk.l.Sales_PurchaseOrderHeader.ToList();
                  Sales_PurchaseOrderHeader mPurchaseOrderHeader = bllPurchaseOrderHeader.Where(h=>h.PurchaseOrderID==m.PurchaseOrderID).FirstOrDefault();
                        this.lblPurchaseOrderNumber.Text = mPurchaseOrderHeader.OrderNumber;
                    }
                    else
                    {
                        this.lblPurchaseOrderNumber.Text = "无";
                    }
                  var bllAccount = mk.l.Sales_Account.ToList();
                  Sales_Account mAccount = bllAccount.Where(h=>h.AccountID==m.AccountID).FirstOrDefault();
                    this.lblAccountName.Text = mAccount.Name;
                    //显示金额
                    this.lblSettlementAmount.Text = m.SettlementAmount.ToString();
                    //显示经办人
                    var bllUser = mk.l.Accounts_Users.ToList();
                Accounts_Users mUser = bllUser.Where(h=>h.UserName==m.Manager).FirstOrDefault();
                    this.lblManagerTrueName.Text = mUser.TrueName;
                    //显示备注
                    this.lblNotes.Text = m.Notes;
                }
                else
                {
                   mk.Alert("只能查看本店的订单！");
                }
            }
        }
    }
}